<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CategoryController
 *
 * @author tuancasi
 */
class CategoryController extends Controller {

    /**
     * show list  
     */
    public function actionIndex() {

	$model = new Category('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['Category'])) {
	    $model->attributes = $_GET['Category'];
	}
	/**
	 * get parent category
	 */
	$aParentCats = Yii::app()->db->createCommand('SELECT category_id, name FROM {{categories}} WHERE parent_id=-1 ')->queryAll();

	$aParentCategories = array();
	foreach ($aParentCats as $cat) {
	    $aParentCategories[$cat['category_id']] = $cat['name'];
	}

	$this->render('index', array(
	    'model' => $model,
	    'aParentCats' => $aParentCategories
	));
    }

    /*     * ***********************************************************************
     * create category
     */

    public function actionCreate() {
	$model = new Category();
	$success = '';
	if (isset($_POST['submit'])) {
	    $model->attributes = $_POST['Category'];

	    //if user want to create parent category
	    if (isset($_POST['has_parent']) && $_POST['has_parent']) {
		$model->parent_id = -1;
	    }

	    if ($model->validate()) {
		if (isset($_FILES['icon']['name'])) {
		    $ext = strtolower(pathinfo($_FILES["icon"]["name"], PATHINFO_EXTENSION));
		    $newName = uniqid() . '.' . $ext;
		    if (move_uploaded_file($_FILES['icon']['tmp_name'], UPLOAD_PATH_CAT . $newName)) {
			$model->icon = $newName;
		    }
		}
		$model->time_created = date('Y-m-d H:i:s', CURR_TIME);
		$model->taxonomy = "post";
		if ($model->save()) {
		    $this->redirect(BACKEND_SITE_URL . 'category');
		}
	    }
	}
	/**
	 * get parent category
	 */
	$aParentCats = Yii::app()->db->createCommand('SELECT category_id, name FROM {{categories}} WHERE parent_id=-1 ')->queryAll();
	$aParentCategories = array();
	foreach ($aParentCats as $cat) {
	    $aParentCategories[$cat['category_id']] = $cat['name'];
	}
	$this->render('create', array(
	    'model' => $model,
	    'aParentCategories' => $aParentCategories,
	    'success' => $success
	));
    }

    /*     * ***********************************************************************
     * view 
     */

    public function actionView() {

	if (isset($_GET['id'])) {
	    $model = Category::model()->findByPk(intval($_GET['id']));
	    $parentName = $model->getParentCatName($model->parent_id);
	    $this->render('view', array(
		'model' => $model,
		'parentName' => $parentName
	    ));
	}
    }

    /*     * ***********************************************************************
     * edit
     */

    public function actionUpdate() {

	if (isset($_GET['id'])) {
	    $success = '';
	    $model = Category::model()->findByPk(intval($_GET['id']));
	    if (isset($_POST['submit'])) {
		$model->attributes = $_POST['Category'];

		//if user want to create parent category
		if (isset($_POST['has_parent']) && $_POST['has_parent']) {
		    $model->parent_id = -1;
		}

		if ($model->validate()) {
		    if (isset($_FILES['icon']['name'])) {

			//delete old file
			if ($model->icon && file_exists(UPLOAD_PATH_CAT . $model->icon)) {
			    unlink(UPLOAD_PATH_CAT . $model->icon);
			}

			$ext = strtolower(pathinfo($_FILES["icon"]["name"], PATHINFO_EXTENSION));
			$newName = uniqid() . '.' . $ext;
			if (move_uploaded_file($_FILES['icon']['tmp_name'], UPLOAD_PATH_CAT . $newName)) {
			    $model->icon = $newName;
			}
		    }

		    $model->time_created = date('Y-m-d H:i:s', CURR_TIME);
		    $model->taxonomy = "post";
		    if ($model->save()) {
			$success = 1;
		    }
		}
	    }
	    /**
	     * get parent category
	     */
	    $aParentCats = Yii::app()->db->createCommand('SELECT category_id, name FROM {{categories}} WHERE parent_id=-1 ')->queryAll();
	    $aParentCategories = array();
	    foreach ($aParentCats as $cat) {
		$aParentCategories[$cat['category_id']] = $cat['name'];
	    }
	    $this->render('update', array(
		'model' => $model,
		'aParentCategories' => $aParentCategories,
		'success' => $success
	    ));
	}
    }

    /*     * ************************************************************************
     * detele category
     */

    public function actionDelete() {
	if (isset($_GET['id'])) {
	    $model = Category::model()->findByPk($_GET['id'])->delete();
	}
    }

    /*     * ***************************************************************************
     * edit slide for category
     */

    public function actionEditBanner($icat = 1) {

	$model = Category::model()->findByPk($icat);
	//get parent category except send a wish
	$aParentCats = Yii::app()->db->createCommand("SELECT category_id, name FROM {{categories}} WHERE parent_id=-1")->queryAll();


	/*
	 * chang img with send a wish
	 */
	if (isset($_POST['changeimg'])) {
	    if (isset($_FILES['image-insert']['name']) && $_FILES['image-insert']['name']) {
		$aSlice = unserialize($model->slice_image);

		//delete old file
		if ($aSlice[0] && file_exists(UPLOAD_PATH_CAT . $aSlice[0])) {
		    unlink(UPLOAD_PATH_CAT . $aSlice[0]);
		}

		$ext = strtolower(pathinfo($_FILES["image-insert"]["name"], PATHINFO_EXTENSION));
		$newName = uniqid() . '.' . $ext;
		if (move_uploaded_file($_FILES['image-insert']['tmp_name'], UPLOAD_PATH_CAT . $newName)) {
		    $aSlice[0] = $newName;
		}
		$model->slice_image = serialize($aSlice);
		$model->save();
	    }
	}
//	Background::addBackground('asasas');
	/*
	 * add img 
	 */
	if (isset($_POST['addimg'])) {
	    if (isset($_FILES['image-insert']['name']) && $_FILES['image-insert']['name']) {

		$ext = strtolower(pathinfo($_FILES["image-insert"]["name"], PATHINFO_EXTENSION));
		$newName = uniqid() . '.' . $ext;
		if (move_uploaded_file($_FILES['image-insert']['tmp_name'], UPLOAD_PATH_BACKGROUND . $newName)) {
		    Background::addBackground($newName);
		}
	    }
	}

	/*
	 * delete img 
	 */
	if (isset($_POST['deleteimg'])) {
	    if (isset($_POST['id-img']) && $_POST['id-img']) {

		foreach ($_POST['id-img'] as $idImg) {
		    //delete old file
		    if ($idImg && file_exists(UPLOAD_PATH_BACKGROUND . $idImg)) {
			unlink(UPLOAD_PATH_BACKGROUND . $idImg);
		    }
		}
		$model->slice_image = serialize($aSlice);
		$model->save();
	    }
	}
	//get imgs
	$aSlice = unserialize($model->slice_image);
	$this->render('editbanner', array(
	    'aSlice' => $aSlice,
	    'aParentCats' => $aParentCats,
	    'icat' => $icat
	));
    }

}
